package com.lc.mail.service.impl;

import com.lc.mail.service.MailService;
import com.study.common.entity.bo.MailBo;
import com.study.common.enums.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/9/26 17:38
 */
@Slf4j
@Service
public class MailServiceImpl implements MailService {

    /**
     * 邮件发送方
     */
    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private JavaMailSender mailSender;

    @Override
    public String normalMail(MailBo bo) {
        try {
            // 创建简单邮件消息
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(bo.getReceivers());
            message.setSubject(bo.getSubject());
            message.setText(bo.getContent());
            mailSender.send(message);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (MailException e) {
            log.error("普通邮件发送失败：{}", e.getMessage());
            return ReturnCodeEnum.MAIL_FAIL.getMsg();
        }
    }

    @Override
    public String htmlMail(MailBo bo) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper mineHelper = new MimeMessageHelper(message, true);
            mineHelper.setFrom(from);
            mineHelper.setTo(bo.getReceivers());
            mineHelper.setSubject(bo.getSubject());
            //邮件内容，true表示带有<附件/html>
            mineHelper.setText(bo.getContent(), true);
            mailSender.send(message);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (MessagingException e) {
            log.error("html短信发送失败：{}", e.getMessage());
            return ReturnCodeEnum.MAIL_FAIL.getMsg();
        }
    }

    @Override
    public String staticMail(MailBo bo) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper mineHelper = new MimeMessageHelper(message, true);
            mineHelper.setFrom(from);
            mineHelper.setTo(bo.getReceivers());
            mineHelper.setSubject(bo.getSubject());
            // 邮件内容，true表示带有<附件/html>
            mineHelper.setText(bo.getContent(), true);
            // TODO 没整完...
            mailSender.send(message);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (MessagingException e) {
            log.error("静态资源短信发送失败：{}", e.getMessage());
            return ReturnCodeEnum.MAIL_FAIL.getMsg();
        }
    }

    @Override
    public String annexMail(MailBo bo, MultipartFile file) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper mineHelper = new MimeMessageHelper(message, true);
            mineHelper.setFrom(from);
            mineHelper.setTo(bo.getReceivers());
            mineHelper.setSubject(bo.getSubject());
            // 邮件内容，true表示带有<附件/html>
            mineHelper.setText(bo.getContent(), true);
            // 文件转换
            File conventFile = multipartFileToFile(file);
            assert conventFile != null;
            FileSystemResource fileResource = new FileSystemResource(conventFile);
            // 添加附件
            mineHelper.addAttachment(Objects.requireNonNull(fileResource.getFilename()), fileResource);
            mailSender.send(message);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (MessagingException e) {
            log.error("附件短信发送失败：{}", e.getMessage());
            return ReturnCodeEnum.MAIL_FAIL.getMsg();
        }
    }

    /**
     * 文件转换，MultipartFile-->File
     */
    private File multipartFileToFile(MultipartFile multiFile) {
        String fileName = multiFile.getOriginalFilename();
        assert fileName != null;
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        try {
            File file = File.createTempFile("annex", prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            log.error("MultipartFile文件转换异常：{}", e.getMessage());
        }
        return null;
    }
}
